package qeug.shsh.opplugin;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.datamodel.events.ChangeData;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;

import java.math.BigDecimal;

//费用名目超过三级后需要使用代码去携带数据，费用类型取最上层，费用细项取末级的上一级
public class ExpenseItemPlugin extends AbstractFormPlugin {

    @Override
    public void propertyChanged(PropertyChangedArgs e) {
        super.propertyChanged(e);
        String fieldKey = e.getProperty().getName();
        if ("expenseitem".equals(fieldKey)) {
            //费用名目
            ChangeData[] changeSet = e.getChangeSet();
            ChangeData changeData = changeSet[0];
            int rowIndex = changeData.getRowIndex();//修改行
            DynamicObject expenseItem = (DynamicObject) changeData.getNewValue();//费用名目
            if (expenseItem == null) {
                this.getModel().setValue("qeug_orderenrtynum", null, rowIndex);
                return;
            }
            int level = (int) expenseItem.get("level");//级次
            if (level > 3) {
                //级别超过三级才使用代码赋值
                int a = 0;
                DynamicObject currentExpenseItem;
                DynamicObject er_expenseItemEdit = null;
                while (true) {
                    if (a == 0) {
                        //第一次进入循环
                        currentExpenseItem = expenseItem;
                    } else {
                        currentExpenseItem = er_expenseItemEdit;
                    }
                    DynamicObject parent = currentExpenseItem.getDynamicObject("parent"); // 获取上级
                    String number = parent.getString("number");
                    QFilter[] qFilters = new QFilter[]{
                            new QFilter("number", QCP.equals, number)
                    };
                    er_expenseItemEdit = BusinessDataServiceHelper.loadSingle("er_expenseitemedit", qFilters);
                    Integer level1 = (Integer) er_expenseItemEdit.get("level");
                    if (level1.equals(1)) {
                        break; // 当 level1 等于 1 时，跳出循环
                    } else {
                        a++;
                    }
                }
                this.getModel().setValue("qeug_expensetype", er_expenseItemEdit, rowIndex);//费用类型，取最上层

                DynamicObject parent = expenseItem.getDynamicObject("parent"); // 获取末级的上级
                this.getModel().setValue("qeug_expensedetail", parent, rowIndex);//费用细项，取末级的上一层
            }
            DynamicObject costcompany = (DynamicObject)this.getModel().getValue("costcompany");//费用承担公司
            QFilter[] q1 = new QFilter[]{new QFilter("qeug_expenseitem.number", "=", expenseItem.getString("number")),
                    new QFilter("qeug_company.number", "=", costcompany.getString("number"))};
            DynamicObject assistantdata = BusinessDataServiceHelper.loadSingle("bos_assistantdata_detail",q1);
            this.getModel().setValue("qeug_orderenrtynum", assistantdata, rowIndex);//给内部订单号塞值
        }
    }
}

